﻿
using System ;


namespace MskEdit.Sdk
{
    public struct SdkSubrange {

        #region Private data

        private int start_ ;
        private int length_ ;

        #endregion 
 
        #region Private validation
 
        private static bool IsValidStart( int start ) { 
        
            if ( 0 > start )
                return false ;
                
            return true ;            
        } 


        private static bool IsValidLength( int length ) { 
        
            if ( 0 > length )
                return false ;
                
            return true ;
        }


        private static bool IsValidRange( int start, int length ) { 
        
            if ( ! IsValidStart( start ) )
                return false ;
        
            if ( ! IsValidLength( length ) )
                return false ;
                
            return true ;
        }
        
        #endregion

        #region Public operators

        public static SdkSubrange operator & ( SdkSubrange s1, SdkSubrange s2 ) {
        
            int begin = Math.Max( s1.Begin, s2.Begin ) ;
            int end   = Math.Min( s1.End  , s2.End   ) ;
        
            return new SdkSubrange( begin, end - begin ) ;
        }

        #endregion

        #region Public foundation

        public SdkSubrange( int start, int length ) {
            start_ = start ;
            length_ = length ;
        }
        
        
        public override String ToString() {
            return start_.ToString() + "[" + length_.ToString() + "]" ;
        }

        #endregion

        #region Public validation

        public bool IsEmpty() {
                        
            return length_ == 0 ;
        }


        public bool IsValid() {
        
            if ( ! IsValidRange( start_, length_ ) )
                return false ;
                
            return true ;
        }
        
        
        public bool IsSubrangeOf( int length ) {
                
            if ( ( start_ + length_ ) > length )
                return false ;
                
            return true ;
        }

              
        public bool Contains( int value ) {
        
            if ( value < start_ )
                return false ;
        
            if ( value >= ( start_ + length_ ) )
                return false ;
                
            return true ;
        }

        #endregion

        #region Public properties

        public int Start {
            get { return start_ ; }
        }


        public int Length {
            get { return length_ ; }
        }
 
 
        public int Begin {
            get { return start_ ; }
        }
 
 
        public int End {
            get { return start_ + length_ ; }
        }


        public int First {
            get { return start_ ; }
        }
 
 
        public int Last {
            get { return start_ + length_ - 1 ; }
        }

        #endregion     
       
    }
}
